package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"note.zhiwangyunchuang.com/dto"
	"note.zhiwangyunchuang.com/model"
	"note.zhiwangyunchuang.com/network"
	"note.zhiwangyunchuang.com/service"
	"note.zhiwangyunchuang.com/utils"
	"os"
	"path"
	"strconv"
	"time"
)

var ossPath string = "https://zwyc-note-oss.oss-cn-beijing.aliyuncs.com"

func NoteList(ctx *gin.Context) {
	noteList := service.GetNoteList()
	var noteSlice []dto.NoteDto
	fmt.Println(noteList)
	for _, note := range noteList {
		noteSlice = append(noteSlice, dto.ToNoteDto(note))
	}
	network.Success(ctx, "成功", gin.H{
		"note_list": noteSlice,
	})
}

//	@Tags			Note
//	@Id				1
//	@Summary		笔记详情接口
//	@Description	根据笔记id获取笔记详情
//	@Produce		json
//	@Param			id	path		int	true	"ID"
//	@Success		200	{object}	network.ResponseParam{}
//	@Failure		422	{object}	network.ResponseParam{}
//	@Router			/v1/note/{id} [get]
func NoteInfo(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	if id <= 0 {
		network.Fail(ctx, "id不正确", gin.H{})
		return
	}

	info, err := service.GetNoteInfoById(id)
	if err != "" {
		network.Fail(ctx, err, gin.H{})
		return
	}
	network.Success(ctx, "成功", gin.H{
		"note_info": dto.ToNoteDto(info),
	})
}

func NoteAdd(ctx *gin.Context) {
	var note = model.Note{}
	ctx.Bind(&note)
	fmt.Println("note======>", note)
	note.Title = utils.RandomString(5)
	if note.Content == "" {
		network.Fail(ctx, "内容必传", gin.H{})
		return
	}
	if note.Type <= 0 {
		network.Fail(ctx, "类型不正确", gin.H{})
		return
	}
	currentUser := ctx.MustGet("user").(model.User)
	note.UserId = int(currentUser.ID)
	lastId, err := service.AddOneNote(note)
	if err != "" {
		network.Fail(ctx, err, gin.H{})
		return
	}
	network.Success(ctx, "成功", gin.H{
		"last_id": lastId,
	})
}

func NoteEdit(ctx *gin.Context) {
	var note model.Note
	id, _ := strconv.Atoi(ctx.Param("id"))
	if id <= 0 {
		network.Fail(ctx, "id不正确", gin.H{})
		return
	}
	ctx.Bind(&note)
	_, err := service.EditNoteById(id, note)
	if err != "" {
		network.Fail(ctx, err, gin.H{})
		return
	}
	network.Success(ctx, "修改成功", gin.H{})
}

func NoteDel(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	if id <= 0 {
		network.Fail(ctx, "id不能为空", gin.H{})
		return
	}
	_, err := service.DeleteNoteById(id)
	if err != "" {
		network.Fail(ctx, err, gin.H{})
		return
	}
	network.Success(ctx, "删除成功", gin.H{})
}

func UpAudio(ctx *gin.Context) {
	file, _ := ctx.FormFile("file")
	dirPath := fmt.Sprintf("./media/%d-%d-%d", time.Now().Year(), time.Now().Month(), time.Now().Day())
	os.MkdirAll(dirPath, 0777)
	fileName := fmt.Sprintf("%d%s", time.Now().Unix(), path.Ext(file.Filename))
	filePath := fmt.Sprintf("%s/%s", dirPath, fileName)
	err := ctx.SaveUploadedFile(file, filePath)
	if err != nil {
		log.Println("上传出错", err)
	}
	// 上传到oss
	//fileName = "zaocan.wav"
	utils.OssUpload(fileName, filePath)
	ossFilePath := ossPath+"/audio/" + fileName
	content := utils.OssIdentify(ossFilePath)
	log.Println("识别文字：",content)
	network.Success(ctx, "成功", gin.H{
		"code": http.StatusOK,
		"file_path" : ossFilePath,
		"content" : content,
	})
}

func IdentifyAudio(ctx *gin.Context) {
	filePath := ctx.PostForm("path")
	content := utils.OssIdentify(filePath)
	network.Success(ctx,"成功",gin.H{
		"content" : content,
	})
}
